﻿namespace LiteBlog.Common.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;

    public class Stat
    {
        public int FeedCount
        {
            get
            {
                if (this.Feeds != null && this.Feeds.Count > 0)
                {
                    return this.Feeds.Values.Max<int>();
                }

                return 0;
            }
        }

        public int Hits { get; set; }
        public Dictionary<string, PostInfo> PageVisits { get; set; }
        public IEnumerable<Tuple<string, int>> MonthlyViews { get; set; }
        public int ViewsThisMonth { get; set; }
        public Dictionary<string, int> Feeds { get; set; }
        public string Path { get; set; }
        public int Visitors { get; set; }
        public int Visits { get; set; }

        public Stat Clone()
        {
            var stat = new Stat();
            stat.Hits = this.Hits;
            stat.Visits = this.Visits;
            stat.Visitors = this.Visitors;
            stat.Path = this.Path;
            stat.ViewsThisMonth = this.ViewsThisMonth;

            stat.PageVisits = new Dictionary<string, PostInfo>();
            foreach (var kvpVisits in PageVisits)
            {
                stat.PageVisits.Add(kvpVisits.Key, kvpVisits.Value);
            }

            stat.Feeds = new Dictionary<string, int>();
            foreach (var kvpFeeds in Feeds)
            {
                stat.Feeds.Add(kvpFeeds.Key, kvpFeeds.Value);
            }

            var monthlyViews = new List<Tuple<string, int>>();
            foreach (var monthlyView in MonthlyViews)
            {
                monthlyViews.Add(monthlyView);
            }
            stat.MonthlyViews = monthlyViews;

            return stat;
        }

        public static string GetFeedKey(DateTime date)
        {
            return date.ToString("ddMMyyyy", CultureInfo.InvariantCulture);
        }

        public static string GetMonthFromKey(string key)
        {
            DateTime dt = DateTime.MinValue;
            DateTime.TryParseExact(
                key, "MMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
            if (dt != DateTime.MinValue)
            {
                return dt.ToString("MMM yyyy", CultureInfo.InvariantCulture);
            }

            return string.Empty;
        }

        public static string GetMonthKey(DateTime date)
        {
            return date.ToString("MMyyyy", CultureInfo.InvariantCulture);
        }

        public static bool IsFeedKey(string key)
        {
            DateTime dt = DateTime.MinValue;
            DateTime.TryParseExact(
                key, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
            if (dt == DateTime.MinValue)
            {
                return false;
            }

            return true;
        }
    }
}